package jgl.vajra.kingkong.part.mine.activity;

import android.text.TextUtils;

import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;

import org.greenrobot.eventbus.EventBus;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.netty.handler.codec.string.StringEncoder;
import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.bean.NicknamePageBean;
import jgl.vajra.kingkong.databinding.ActivityNicknamePageBinding;
import jgl.vajra.kingkong.event.MapPointEvent;
import jgl.vajra.kingkong.event.ModifyInformationEvent;
import jgl.vajra.kingkong.manager.AccountManager;
import jgl.vajra.kingkong.part.mine.contract.NicknamePageContract;
import jgl.vajra.kingkong.part.mine.viewModel.NicknamePageViewModel;
import jgl.vajra.kingkong.utils.RandomUtils;
import jgl.vajra.kingkong.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.kingkong.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.kingkong.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.kingkong.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.kingkong.widget.retrofithelper.utils.AEACCoder;
import jgl.vajra.kingkong.widget.utils.ToastUtils;
import jgl.vajra.kingkong.widget.utils.UIUtils;

@CreateViewModel(NicknamePageViewModel.class)
public class NicknamePageActivity extends BaseMVVMActivity<NicknamePageViewModel, ActivityNicknamePageBinding> implements NicknamePageContract.View, BaseBindingItemPresenter<NicknamePageBean> {

    private SingleTypeBindingAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_nickname_page;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);
        String nickname = getIntent().getStringExtra("nickname");
        mBinding.etNickname.setText(nickname);

        // 不可嵌套滑动
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerView.setFocusableInTouchMode(false);

        FlexboxLayoutManager flManager = new FlexboxLayoutManager(this);
        flManager.setFlexWrap(FlexWrap.WRAP);
        //设置主轴的方向
        flManager.setFlexDirection(FlexDirection.ROW);
        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(flManager);

        adapter = new SingleTypeBindingAdapter(this, null, R.layout.item_nickname_page);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);

        requestNetData();
    }

    private void requestNetData() {
        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AEACCoder.encrypt(RandomUtils.getRandom()));

        mViewModel.getNicknameData(map);
    }

    //保存
    @Override
    public void onDetermine() {
        String nickname = mBinding.etNickname.getText().toString().trim();
        if (TextUtils.isEmpty(nickname)) {
            ToastUtils.showShort("请输入江湖名号");
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        map.put("nickname", URLEncoder.encode(nickname));

        mViewModel.modifyInformation(map);
    }

    @Override
    public void returnNicknamePageData(List<NicknamePageBean> data) {
        adapter.refresh(data);
    }

    @Override
    public void returnModifyInformationData() {
        String nickname = mBinding.etNickname.getText().toString().trim();
        ToastUtils.showShort("修改成功");
        EventBus.getDefault().post(new ModifyInformationEvent(1,nickname));
        finish();
    }

    @Override
    public void onItemClick(int position, NicknamePageBean itemData) {
        mBinding.etNickname.setText(itemData.description);
    }
}
